/* 
  关于术语说明：
  ”内部模块“叫做”命名空间“
  ”外部模块“叫做”模块“
  也就是说：module X 相当于现在推荐的写法 namespace X
*/

//------------------------所有文件放在一个文件中-------------------------------
// interface StringValidator {
//   isAcceptable(s: string): boolean;
// }

// let lettersRegexp = /^[A-Za-z]+$/;
// let numberRegexp = /^[0-9]+$/;

// class LettersOnlyValidator implements StringValidator {
//   isAcceptable(s: string) {
//     return lettersRegexp.test(s);
//   }
// }

// class ZipCodeValidator implements StringValidator {
//   isAcceptable(s: string) {
//     return s.length === 5 && numberRegexp.test(s);
//   }
// }

// let strings: any[] = ['hello', '90821', '101'];

// let validators: { [s: string]: StringValidator; } = {};
// validators['zip code'] = new ZipCodeValidator();
// validators['letters only'] = new LettersOnlyValidator();

// for (let s of strings) {
//   for (let name in validators) {
//     let isMatch = validators[name].isAcceptable(s);
//     console.log(`${s} ${isMatch ? 'matches' : 'dode not match'} ${name}`);
//   }
// }
//----------------------------使用命名空间的验证器-----------------------------
// namespace Validation {
//   export interface StringValidator {
//     isAcceptable(s: string): boolean;
//   }

//   let lettersRegexp = /^[A-Za-z]+$/;
//   let numberRegexp = /^[0-9]+$/;

//   export class LettersOnlyValidator implements StringValidator {
//     isAcceptable(s: string) {
//       return lettersRegexp.test(s);
//     }
//   }
//   export class ZipCodeValidator implements StringValidator {
//     isAcceptable(s: string) {
//       return s.length === 5 && numberRegexp.test(s);
//     }
//   }
// }
// let strings = ['hello', '98676', 'fgd'];
// let validators: { [s: string]: Validation.StringValidator; } = {};
// validators['zip code'] = new Validation.LettersOnlyValidator();
// validators['letters only'] = new Validation.ZipCodeValidator();

// for (let s of strings) {
//   for (let name in validators) {
//     let isMatch = validators[name].isAcceptable(s);
//     console.log(`${s} ${isMatch ? 'match' : 'does not match'} ${name}`)
//   }
// }
//----------------------------多文件中的命名空间---------------------------
//Validation.tsx
namespace Validation {
  export interface StringValidator {
    isAcceptable(s: string): boolean;
  }
}
//LettersValidator.tsx  三斜杠代表需要加载的依赖项
///<reference path="Validation.tsx" /> 
namespace Validation {
  const lettersRegexp = /^[A-Za-z]+$/;
  export class LettersOnlyValidator implements StringValidator {
    isAcceptable(s: string) {
      return lettersRegexp.test(s);
    }
  }
}
// ZipCodeValidator.tsx
///<reference path="Validator.tsx"/>
namespace Validation {
  const numberRegexp = /^[0-9]+$/;
  export class ZipCodeValidator implements StringValidator {
    isAcceptable(s: string) {
      return s.length === 5 && numberRegexp.test(s);
    }
  }
}
// test.tsx
///<reference path="Validation.tsx"/>
///<reference path="LettersValidator.tsx"/>
///<reference path="ZipCodeValidator.tsx"/>
let strings = ['hello', '23132', 'lij'];
let validators: { [s: string]: Validation.StringValidator } = {};
validators['zip code'] = new Validation.ZipCodeValidator();
validators['letter only'] = new Validation.LettersOnlyValidator();

for (let s of strings) {
  for (let name in validators) {
    const isMatch = validators[name].isAcceptable(s);
    console.log(`${s} ${isMatch ? 'match' : 'does not match'} ${name}`);
  }
}
//---------------------------外部命名空间------------------------------
declare namespace D3 {
  export interface Selectors {
    select: {
      (selector: string): Selection;
    }
  }
  export interface Event {
    x: number;
    y: number;
  }
  export interface Base extends Selectors {
    evnet: Event;
  }
}
declare let d3: D3.Base;